package com.situ.personal.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.personal.dao.DepartmentDao;
import com.situ.personal.entity.Department;
import com.situ.personal.util.JSONUtil;
import com.situ.personal.vo.DepartmentCountVO;

@WebServlet("/department")
public class DepartmentServlet extends HttpServlet{
	private DepartmentDao departmentDao = new DepartmentDao();

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String method = req.getParameter("method");
		switch (method) {
		case "selectAllByJson":
			selectAllByJson(req, resp);
			break;
		case "count":
			count(req, resp);
			break;

		default:
			break;
		}
	}

	private void count(HttpServletRequest req, HttpServletResponse resp) {
		List<DepartmentCountVO> list = departmentDao.count();
		JSONUtil.array2Json(list, resp);
	}

	private void selectAllByJson(HttpServletRequest req, HttpServletResponse resp) {
		System.out.println("DepartmentServlet.selectAllByJson()");
		List<Department> list = departmentDao.selectAll();
		
		JSONUtil.array2Json(list, resp);
	}
}
